package org.fluidripple.socket.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.fluidripple.socket.inbound.ClientEventHandler;
import org.fluidripple.socket.inbound.DynamicDecoderHandler;
import org.fluidripple.socket.outbound.ProtocolEncoderHandler;

import java.util.concurrent.TimeUnit;


public class ChannelInitializerImpl extends ChannelInitializer<NioSocketChannel> {
    @Override
    protected void initChannel(NioSocketChannel channel){
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast(new IdleStateHandler(0,0,1, TimeUnit.MINUTES));
        pipeline.addLast(ProtocolEncoderHandler.NAME,new ProtocolEncoderHandler());
        pipeline.addLast(ClientEventHandler.name, new ClientEventHandler());
//        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(1);
//        buffer.writeBytes(ByteBufUtil.decodeHexDump("F0"));
//        pipeline.addLast("aaa",new DelimiterBasedFrameDecoder(100,buffer ));
        pipeline.addLast(DynamicDecoderHandler.name,new DynamicDecoderHandler());
    }
}
